/**
 * Created by apple on 16/10/27.
 */
var lookAtLeftTop = "";


//页面滚动时执行
window.onscroll = function () {
    lookAtLeftTop = $('.lookat-left').offset().top;
    // 看Ta的主页左侧
    if ($(".lookat-left").length > 0) {
        if (winScrollTop >= lookAtLeftTop) {
            $(".left-box").addClass('lookat-left-fixed');
            if ($(".left-box").height() > winHeight - 286) {
                if (winScrollTop >= bodyHeight - winHeight - 236) {//侧边栏定位
                    $('.lookat-left-fixed').css({
                        "top": "auto",
                        "bottom": winHeight + winScrollTop + 256 - bodyHeight,
                    });
                }
                else {
                    $('.lookat-left-fixed').css("top", "50px");
                }
            }
        }
        else {
            $(".left-box").removeClass('lookat-left-fixed');
            $('.left-box').attr('style', '');
        }
    }
};


//作者信息
function lookat_uesr_info() {
    $.get(url_link + 'users/' + check_user, {my_id: user_id}, function (data) {
        try {
            var obj = JSON.parse(data);
            var uesr_list = "";
            if (obj.code == 0) {
                uesr_list += "<div class='left-box'><div class='head-pic'>";
                if (obj.data.sex = 1) {
                    uesr_list += "<b class='male-mid'></b>";
                } else {
                    uesr_list += "<b class='female-mid'></b>"
                }
                uesr_list += "<img src=" + obj.data.avatar + ">" +
                    "</div>" +
                    "<div class='user-name'>" +
                    "<span>" + obj.data.nickname + "<em>LV:" + obj.data.level + "</em></span>" +
                    "</div>" +
                    "<ul class='user-count'>" +
                    "<li><a href='friend.html?user_id=" + obj.data.user_id + "'>" + obj.data.follow_count + "</a><span>关注</span></li>" +
                    "<span class='line'></span>" +
                    "<li><a href='fans.html?user_id=" + obj.data.user_id + "'>" + obj.data.fans_count + "</a><span>粉丝</span></li>" +
                    "<span class='line'></span>" +
                    "<li><a href='article.html?user_id=" + obj.data.user_id + "'>" + obj.data.article_count + "</a><span>作品</span></li>" +
                    "</ul>";
                if (obj.data.has_black == 1) {
                    //已经被加入到黑名单中
                    uesr_list += "<div class='button-area-blacklist'>" +
                        "<a href='javascript:;' class='white-button remove-blacklist-btn' id='blacklist_button' data-target=" + obj.data.user_id + "><i></i>移出黑名单</a>" +
                        "<a href='javascript:;' class='white-button report-btn' data-report-person-user-id=" + obj.data.user_id + "><i></i>举报</a>" +
                        "<a href='javascript:;' class='white-button private-btn message-button' id='private_letter_button'><i></i>私信</a></div>"
                } else {
                    uesr_list += "<div class='button-area' style='display: block;'>";
                    if (obj.data.has_follow == 1) {
                        uesr_list += "<a href='javascript:;' class='white-button un-follow-btn' id='pay_attention_button' data-user-id=" + user_id + " data-target-id=" + obj.data.user_id + "><i></i>取消关注</a>"
                    } else {
                        uesr_list += "<a href='javascript:;' class='white-button follow-btn' id='pay_attention_button' data-user-id=" + user_id + " data-target-id=" + obj.data.user_id + "><i></i>关注</a>"
                    }
                    uesr_list += "<a href='javascript:;' class='white-button report-btn' data-report-person-user-id=" + obj.data.user_id + "><i></i>举报</a>" +
                        "<a href='javascript:;'  class='white-button add-blacklist-btn' id='blacklist_button' data-target=" + obj.data.user_id + "><i></i>拉黑</a>" +
                        "<a href='javascript:;'  class='white-button private-btn message-button' id='private_letter_button'><i></i>私信</a></div>"
                }
                uesr_list += "<div class='user-info'>" +
                    "<h6>个性签名：</h6><div>" + obj.data.sign + "</div>";
                if (obj.data.birth == undefined) {
                    uesr_list += "<h6>生日：</h6><div>保密</div>"
                } else {
                    uesr_list += "<h6>生日：</h6><div>" + obj.data.birth.substring(0, 10) + "</div>"
                }
                uesr_list += "<h6>注册时间：</h6><div>" + obj.data.createdDate.substring(0, 10) + "</div>";

                if (obj.data.area == undefined) {
                    uesr_list += "<h6>所在地：</h6><div>保密</div>"
                } else {
                    uesr_list += "<h6>所在地：</h6><div>" + obj.data.area + "</div>"
                }
                uesr_list += "</div></div>";

                $('.lookat-left').html(uesr_list);

            } else {
            }
        } catch (e) {
        }
    })
}


//关注的人
function lookat_friend() {
    $.ajax({
        type: 'get',
        url: url_link + 'follows',
        data: data_motocycle,
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var lookat_friend_list = "";
                total = obj.data.total;
                if (obj.code == 0) {
                    $('.small-title .current em').html(obj.data.total + "人");
                    if (obj.data.total == 0) {
                        if (data_motocycle.type == 0) {
                            //他的关注
                            lookat_friend_list += "<h2 class='none_info' style='margin: 200px 0 ;'>还没有任何人关注Ta哦</h2>"
                        } else {
                            lookat_friend_list += "<h2 class='none_info' style='margin: 200px 0 ;'>Ta还没有关注任何人哦</h2>"
                        }
                    } else {
                        $.each(obj.data.rows, function (i, item) {
                            lookat_friend_list += "<li class='lookat-friend'>" +
                                "<dl>" +
                                "<dt>" +
                                "<a href='article.html?user_id=" + item.user_id + "' target='_blank' class='headpic'>"
                            if (item.user.avatar) {
                                lookat_friend_list += "<img src=" + item.user.avatar + " style='width: 90px;height: 90px;'>"
                            } else {
                                lookat_friend_list += "<img src='../../image/base/none.png' style='width: 90px;height: 90px;'>"
                            }
                            lookat_friend_list += "</a>";
                            if (item.user.sex == 1) {
                                lookat_friend_list += "<i class='male-mid'></i>"
                            } else {
                                lookat_friend_list += "<i class='female-mid'></i>"
                            }
                            lookat_friend_list += "</dt>" +
                                "<dd class='friend-info'>" +
                                "<h5>" +
                                "<a href='article.html?user_id=" + item.user_id + "' target='_blank'>" + item.user.nickname + "<em class='level'>LV." + item.user.level + "</em></a>" +
                                "</h5>" +
                                "<ul class='friend-info-count'>" +
                                "<li><a href='friend.html?user_id=" + item.follow_id + "'><span>关注</span>" + item.user.follow_count + "</a></li>" +
                                "<span class='line'></span>" +
                                "<li><a href='fans.html?user_id=" + item.follow_id + "'><span>粉丝</span>" + item.user.fans_count + "</a></li>" +
                                "<span class='line'></span>" +
                                "<li><a href='article.html?user_id=" + item.follow_id + "'><span>作品</span>" + item.user.article_count + "</a></li>" +
                                "</ul>" +
                                "<p class='signature'>个性签名：" + item.user.sign + "</p>" +
                                "</dd>";
                            //说明不是自己的账号
                            lookat_friend_list += "<dd class='button-area'>";
                            if (item.user.user_id != data_motocycle.my_id) {
                                if (item.user.has_follow == 1) {
                                    //互相关注
                                    lookat_friend_list += "<a href='javascript:;' class='white-button un-follow-btn' " +
                                        "id='pay_attention_button' style='margin: 0 10px;' data-user-id=" + data_motocycle.my_id + " " +
                                        "data-target-id=" + item.user.user_id + "><i></i>取消关注</a>"
                                } else {
                                    lookat_friend_list += "<a href='javascript:;' class='white-button follow-btn' " +
                                        "id='pay_attention_button' style='margin: 0 10px;' data-user-id=" + data_motocycle.my_id + " " +
                                        "data-target-id=" + item.user.user_id + "><i></i>关注</a>"
                                }
                                lookat_friend_list += "<a href='javascript:;' class='white-button private-btn message-button' id='private_letter_button'><i></i>发私信</a>"
                            } else {
                                lookat_friend_list += "<a href='../center/home.html' target='_blank' class='white-button user-home-btn'><i></i>个人中心</a>"
                            }
                            lookat_friend_list += "</dd></dl></li>"
                        });
                    }
                    $('.lookat-friend-list ul').html(lookat_friend_list);
                } else {
                }
            } catch (e) {
            }
        }
    })

}


//作品
function lookat_article() {
    $.ajax({
        type: 'get',
        url: url_link + 'articles',
        data: data_motocycle,
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                var lookat_article_list = "";
                total = obj.data.total;
                if (obj.code == 0) {
                    $('.small-title em').html("共有" + obj.data.total + "篇");
                    $.each(obj.data.rows, function (i, item) {
                        lookat_article_list += "<li>";
                        if (item.mototype_name) {
                            lookat_article_list += "<a href='../discover_theme.html?mototype_id=" + item.mototype_id + "' class='topic-lab' target='_blank' data_mototype_id=" + item.mototype_id + ">" + item.mototype_name + "</a>"
                        }
                        lookat_article_list += "<a href='../publish/show.html?article_id=" + item.article_id + "' class='img-box' target='_blank'>"

                        if (item.cover) {
                            lookat_article_list += "<img src='" + item.cover + "'>";
                        } else {
                            var _content = item.content.split("$$##MotorTravel##$$");
                            var _count = 0;
                            for (var _a = 0; _a < _content.length; _a++) {
                                var m = _content[_a].substring(0, 13);
                                if (m == "$$##Image##$$") {
                                    //有图
                                    _count = 1;
                                    var _content_image_description = _content[_a].split("$$##ImageDescription##$$");
                                    var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                    lookat_article_list += "<img src=" + _content_image_url[0].substring(13, _content_image_url[0].length) + " >"
                                    break;
                                }
                            }
                            //纯文字,没有图的情况下,使用备用图片
                            if (_count == 0) {
                                lookat_article_list += "<img src='../../image/base/none_img.png'>"
                            }
                        }

                        lookat_article_list += "</a>" +
                            "<h5 class='thread-title'>" +
                            "<a href='../publish/show.html?article_id=" + item.article_id + "' target='_blank'>";
                        if (item.is_nice == 1) {
                            lookat_article_list += "<i class='essence'></i>"
                        }
                        lookat_article_list += item.title + "</a>" +
                            "</h5>" +
                            "<div class='thread-info'>" +
                            "<div class='thread-count'>" +
                            "<span class='click-count'><i></i>" + item.see_count + "</b></span>" +
                            "<span class='comment-count'><i></i>" + item.comment_count + "</span>" +
                            "<span class='praise-count'><i></i>" + item.gift_count + "</span>" +
                            "</div></div></li>"
                    });
                    $('.lookat-thread-list').html(lookat_article_list);
                    //主题颜色
                    topicColor();
                } else {
                }
            } catch (e) {

            }
        }
    })
}


//Ta的关注  or  Ta的粉丝
$(document).on('click', '.small-title a', function () {
    var _a = $(this).html().substring(0, 5);
    if (_a == "Ta的关注") {
        window.location.href = "friend.html?user_id=" + check_user
    } else if (_a == "Ta的粉丝") {
        window.location.href = "fans.html?user_id=" + check_user
    }
});


//选择"所有帖子","精华帖子","被选题收录"
$(document).on('click', '.choose_kind li', function () {
    //排序以"最新发布"为基础排序
    $('.comment-nav a').css('color', 'black');
    $('.comment-nav a:first').css('color', '#2175d0');
    //赋值
    var _text = $(this).html();
    $('.current-filter').text(_text);
    $('.small-title a:first').text(_text);
    //画页面
    var _data_id = $(this).attr('data-id');
    if (_data_id == 1) {
        //所有帖子
        data_motocycle = {
            user_id: check_user,
            page: 1,
            rows: 15
        }

    } else if (_data_id == 2) {
        //精华帖子
        data_motocycle = {
            user_id: check_user,
            page: 1,
            rows: 15,
            is_nice: 1
        }
    } else if (_data_id == 3) {
        //被选题收录
        data_motocycle = {
            user_id: check_user,
            page: 1,
            rows: 15,
            is_theme: 1
        }
    }
    lookat_article();
    //分页
    var _pagenum = Math.ceil(total / data_motocycle.rows);
    if (_pagenum > 1) {
        //需要分页展示
        $('.pageNum').show();
        $(".pageNum").createPage({
            pageCount: _pagenum,
            current: data_motocycle.page,
            backFn: function (p) {
                //单击回调方法，p是当前页码
                data_motocycle.page = p;
                lookat_article();
            }
        });
    } else {
        $('.pageNum').hide();
    }
});


//选择"最新发布","点击最高","评论最多"
$(document).on('click', '.comment-nav a', function () {
    $('.comment-nav a').css('color', 'black');
    $(this).css('color', '#2175d0');
    var _text = $(this).html();
    is_nice = data_motocycle.is_nice;
    is_theme = data_motocycle.is_theme;
    //精华帖子
    if (is_nice) {
        if (_text == "最新发布") {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                is_nice: is_nice
            }
        } else if (_text == "点击最高") {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                is_nice: is_nice,
                mostSee: 1
            }
        } else {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                is_nice: is_nice,
                mostComment: 1
            }
        }
    }
    //被选题收录
    if (is_theme) {
        if (_text == "最新发布") {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                is_theme: is_theme
            }
        } else if (_text == "点击最高") {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                is_theme: is_theme,
                mostSee: 1
            }
        } else {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                is_theme: is_theme,
                mostComment: 1
            }
        }
    }
    //全部帖子
    if (!is_nice && !is_theme) {
        if (_text == "最新发布") {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15
            }
        } else if (_text == "点击最高") {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                mostSee: 1
            }
        } else {
            data_motocycle = {
                user_id: check_user,
                page: 1,
                rows: 15,
                mostComment: 1
            }
        }
    }
    lookat_article();
    //分页
    var _pagenum = Math.ceil(total / data_motocycle.rows);
    if (_pagenum > 1) {
        //需要分页展示
        $('.pageNum').show();
        $(".pageNum").createPage({
            pageCount: _pagenum,
            current: data_motocycle.page,
            backFn: function (p) {
                //单击回调方法，p是当前页码
                data_motocycle.page = p;
                lookat_article();
            }
        });
    } else {
        $('.pageNum').hide();
    }
});


//加入黑名单
$(document).on('click', '#blacklist_button', function () {
    var _target_id = $(this).attr('data-target');
    var _html = $(this).html();
    _html = _html.substring(7, _html.length);
    if (_html == "拉黑") {
        layer.msg('确定要将Ta加入黑名单中？', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.post(url_link + 'blacklists', {
                    user_id: user_id,
                    target_id: _target_id
                }, function (data) {
                    var obj = JSON.parse(data);
                    if (obj.code == 0) {
                        layer.msg("加入黑名单成功!", {time: 1000});
                        setTimeout(function () {
                            lookat_uesr_info();
                        }, 1000)

                    }
                })
            }
        });
    } else {
        layer.msg('确定要将Ta从黑名单中移除？', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.post(url_link + 'blacklists', {
                    user_id: user_id,
                    target_id: _target_id
                }, function (data) {
                    var obj = JSON.parse(data);
                    if (obj.code == 0) {
                        layer.msg("移除黑名单成功!", {time: 1000});
                        setTimeout(function () {
                            lookat_uesr_info();
                        }, 1000)

                    }
                })
            }
        });
    }
});